package it.antonio.ilpizze.db.statistics;

import it.antonio.ilpizze.db.core.StatisticRecord;

import java.util.List;

public class Measures {
	public static StatisticMetric<Long> maxTime(){
		return new StatisticMetric<Long>() {
			
			@Override
			public Long calculate(List<StatisticRecord> records) {
				if(records.size() == 0) return 0l;
				Long max = Long.MIN_VALUE;
				for(StatisticRecord record: records){
					if(record.time() > max) max = record.time();
				}
				return max;
			}
		};
	}
	
	public static StatisticMetric<Long> minTime(){
		return new StatisticMetric<Long>() {
			
			@Override
			public Long calculate(List<StatisticRecord> records) {
				if(records.size() == 0) return 0l;
				Long min = Long.MAX_VALUE;
				for(StatisticRecord record: records){
					if(record.time() < min) min = record.time();
				}
				return min;
			}
		};
	}
	
	public static StatisticMetric<Long> averageTime(){
		return new StatisticMetric<Long>() {
			
			@Override
			public Long calculate(List<StatisticRecord> records) {
				if(records.size() == 0) return 0l;
				double result = 0;
				for(StatisticRecord record: records){
					result+= record.time();
				}
				return (long) result / records.size();
			}
		};
	}
	public static StatisticMetric<Long> lastTime(){
		return new StatisticMetric<Long>() {
			
			@Override
			public Long calculate(List<StatisticRecord> records) {
				if(records.size() == 0) return 0l;
				return records.get(records.size() - 1).time();
			}
		};
	}
	
	public static StatisticMetric<Integer> numTimes(){
		return new StatisticMetric<Integer>() {
			
			@Override
			public Integer calculate(List<StatisticRecord> records) {
				return records.size();
			}
		};
	}
	
	public static MeasureMetric<Long> databaseSize(){
		return new MeasureMetric<Long>() {
			
			@Override
			public Long calculate(MeasureContext context) {
				return context.dbSize();
			}
		};
	}
}
